package com.ruoyi.commodity.controller;

import com.ruoyi.commodity.domain.CommodityCategory;
import com.ruoyi.commodity.service.ICommodityCategoryService;
import com.ruoyi.common.core.constant.CommodityCategoryConstants;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: Steven
 * @CreateTime: 2023-07-12  14:50
 */
@RestController
@RequestMapping("/category")
public class CommodityCategoryController extends BaseController {
    @Autowired
    private ICommodityCategoryService commodityCategoryService;

    /**
     * @description:获取分类列表
     * @author: Steven
     * @date: 2023/7/12 0012 16:34
     * @param: [commodityCategory]
     * @return: com.ruoyi.common.core.web.domain.AjaxResult
     **/
    @Log(title = "商品分类管理")
    @GetMapping("/list")
    public AjaxResult list (CommodityCategory commodityCategory){
        List<CommodityCategory> commodityCategoryList = commodityCategoryService.selectCommodityCategoryList(commodityCategory);
        return success(commodityCategoryList);
    }


    @GetMapping("/category/exclude/{categoryId}")
    public AjaxResult excludeChild(@PathVariable(value = "categoryId",required = false)Long categoryId){
        List<CommodityCategory> commodityCategories = commodityCategoryService.selectCommodityCategoryList(new CommodityCategory());
        commodityCategories.removeIf(d->d.getCategoryId().intValue()==categoryId || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), categoryId + ""));
        return success(commodityCategories);
    }

    /**
     * @description:根据分类编号获取详细信息
     * @author: Steven
     * @date: 2023/7/12 0012 16:39
     * @param: [categoryId]
     * @return: com.ruoyi.common.core.web.domain.AjaxResult
     **/
    @Log(title = "商品分类管理")
    @GetMapping(value = "/{categoryId}")
    public AjaxResult getInfo(@PathVariable Long categoryId)
    {
        return success(commodityCategoryService.selectCommodityCategoryById(categoryId));
    }

    /**
     * @description:新增商品分类
     * @author: Steven
     * @date: 2023/7/12 0012 22:40
     * @param: [commodityCategory]
     * @return: com.ruoyi.common.core.web.domain.AjaxResult
     **/
    @Log(title = "商品分类管理",businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody CommodityCategory commodityCategory){
        if (!commodityCategoryService.checkCommodityCategoryNameUnique(commodityCategory))
        {
            return error("新增商品" + commodityCategory.getCategoryName() + "失败，分类名称已存在");
        }
        commodityCategory.setCreateBy(SecurityUtils.getUsername());
        return toAjax(commodityCategoryService.insertCommodityCategory(commodityCategory));

    }

    /**
     * @description:更新商品
     * @author: Steven
     * @date: 2023/7/13 0013 9:37
     * @param: [commodityCategory]
     * @return: com.ruoyi.common.core.web.domain.AjaxResult
     **/

    @Log(title = "商品分类管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody CommodityCategory commodityCategory)
    {
        Long categoryId = commodityCategory.getCategoryId();
        if (!commodityCategoryService.checkCommodityCategoryNameUnique(commodityCategory))
        {
            return error("修改分类'" + commodityCategory.getCategoryName() + "'失败，分类名称已存在");
        }
        else if (commodityCategory.getParentId().equals(categoryId))
        {
            return error("修改分类'" + commodityCategory.getCategoryName()  + "'失败，上级分类不能是自己");
        }
        else if (StringUtils.equals(CommodityCategoryConstants.CATEGORY_DISABLE, commodityCategory.getStatus()) && commodityCategoryService.selectNormalChildrenCategoryById(categoryId) > 0)
        {
            return error("该分类包含未停用的子分类！");
        }
        commodityCategory.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(commodityCategoryService.updateCommodityCategory(commodityCategory));
    }

    @Log(title = "商品分类管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{categoryId}")
    public AjaxResult remove(@PathVariable Long categoryId)
    {
        if(commodityCategoryService.hasChildByCategoryId(categoryId)){
            return warn("存在下级分类，不允许删除");
        }
        if(commodityCategoryService.checkCategoryExistCommodity(categoryId)){
            return warn("分类下存在商品，不允许删除");
        }
        return toAjax(commodityCategoryService.deleteCategoryById(categoryId));
    }

    @GetMapping("/categoryTree")
    public AjaxResult categoryTree(CommodityCategory commodityCategory){
        return success(commodityCategoryService.selectCategoryTreeList(commodityCategory));
    }


}
